import django_filters
import random
from .models import ReceiverPlace, VaildArea, GoodsType, Logistics, CartItem, Goods, OrderIncome, WomaiOrder, \
    TransferRecord, WomaiChildOrder, Banner


# banner类型筛选
class BannerFilter(django_filters.FilterSet):
    type = django_filters.ChoiceFilter(help_text="类型", choices=Banner.TYPE, label="类型")

    class Meta:
        model = Banner
        fields = ("type",)


# 收货地址
class ReceiverPlaceFilter(django_filters.FilterSet):
    default = django_filters.NumberFilter(field_name="default", help_text="是否是默认地址")

    class Meta:
        model = ReceiverPlace
        fields = ("default",)


# 订单过滤
class OrderFilter(django_filters.FilterSet):
    order_status = django_filters.NumberFilter(method='order_status_filter')

    def order_status_filter(self, queryset, name, value):
        if int(value) == 0:
            return queryset
        else:
            return queryset.filter(order_status=value)

    class Meta:
        model = WomaiOrder
        fields = ["order_status", ]


# 商品过滤
class GoodsFilter(django_filters.FilterSet):
    county = django_filters.CharFilter(field_name="goodstype__vaildarea__district__name", help_text="需要配送的城市")
    company = django_filters.NumberFilter(field_name="company", help_text="公司id")
    index_pre = django_filters.NumberFilter(field_name="index_pre", help_text="首页专区id")
    system_category = django_filters.NumberFilter(field_name="system_category", help_text="分类id")
    time = django_filters.NumberFilter(field_name="active_start_time__id", help_text="开抢时间")
    goodsname = django_filters.CharFilter(field_name="goodsname", help_text="商品名称", lookup_expr="contains")
    is_vip_bag = django_filters.NumberFilter(field_name="is_vip_bag", help_text="获取会员礼包")
    is_popular = django_filters.NumberFilter(method="filter_is_popular", help_text="猜你喜欢")
    high_rebates = django_filters.NumberFilter(field_name="high_rebates", help_text="是否是高佣金专区")

    def filter_is_popular(self, queryset, name, value):
        if value == 1:
            queryset = queryset.filter(is_vip_bag=0)
            count = queryset.count()
            start = random.randint(0, count - 9)
            return queryset[start: start + 9]
        else:
            return queryset

    class Meta:
        model = Goods
        fields = ["county", "index_pre", "time", "system_category", "is_vip_bag", "is_popular", "high_rebates",
                  "goodsname", "company"]


# 购物车
class CartItemFilter(django_filters.FilterSet):
    """
    购物车搜索
    """

    id = django_filters.CharFilter(field_name="id", method="filter_id")

    class Meta:
        model = CartItem
        fields = ["id", ]

    def filter_id(self, queryset, name, value):
        value = value.split(",")
        queryset = queryset.filter(id__in=value)
        return queryset


# 配送范围
class AreaFilter(django_filters.FilterSet):
    district = django_filters.NumberFilter(label="区县id", field_name="district__code")
    goods_type = django_filters.ChoiceFilter(field_name="goods_type__goods_type", choices=GoodsType.goodsType, )

    class Meta:
        model = VaildArea
        fields = ["district", "goods_type"]
        # fields = ["district", ]


# 物流信息查询
class LogisticsFilter(django_filters.FilterSet):
    order_id = django_filters.CharFilter(field_name="order", label="订单id")

    class Meta:
        model = Logistics
        fields = ["order_id", ]


# 收益查询
class IncomeFilter(django_filters.FilterSet):
    dataType = (
        ("income", 'income'),
        ("sales", 'sales'),
        ("order", 'order'),
    )
    start_time = django_filters.DateFilter(field_name="create_time", label="查询的开始时间", lookup_expr="create_time__gte")
    end_time = django_filters.DateFilter(field_name="create_time", label="查询的结束时间", lookup_expr="create_time__lte")
    submit_state = django_filters.NumberFilter(field_name="submit_state", label="收益状态", help_text="收益状态",
                                               method="filter_submit_state")
    data_type = django_filters.ChoiceFilter(choices=dataType, label="查询数据类型", help_text="查询数据类型",
                                            method="filter_data_type")
    order_type = django_filters.NumberFilter(help_text="订单类型", label="订单类型")

    def filter_submit_state(self, queryset, name, value):
        if value == 0 or value == "0":
            return queryset
        else:
            return queryset.filter(submit_state=value)

    def filter_data_type(self, queryset, name, value):
        return queryset

    class Meta:
        model = OrderIncome
        fields = ["start_time", "end_time", "submit_state", "data_type"]


class TransferFilter(django_filters.FilterSet):
    create_time = django_filters.DateFilter(field_name="create_time", label="查询的结束时间", method='create_time_filter')

    def create_time_filter(self, queryset, name, value):
        queryset = queryset.filter(create_time__year=value.year, create_time__month=value.month)
        return queryset

    class Meta:
        model = TransferRecord
        fields = ["create_time", ]


class ChildOrderFilter(django_filters.FilterSet):
    afterSale = ((1, "发起售后"),)
    after_type = django_filters.ChoiceFilter(field_name="after_type", label="退款类型", choices=WomaiChildOrder.afterType)
    after_sale = django_filters.ChoiceFilter(field_name="after_sale", label="退款状态", choices=WomaiChildOrder.afterSale)
    oid = django_filters.CharFilter(field_name="oid", label="退款订单号", lookup_expr="contains")
    # 用于区分后台和用户的
    after_sale_list = django_filters.ChoiceFilter(label="移动端售后列表", choices=afterSale, method="filter_after_sale_list")

    def filter_after_sale_list(self, queryset, name, value):
        if value == 1 or value == "1":
            queryset = queryset.exclude(after_sale=0)
            return queryset
        else:
            return queryset

    class Meta:
        model = WomaiChildOrder
        fields = ["after_type", "after_sale", "oid"]


class SendGoodsFilter(django_filters.FilterSet):
    tid = django_filters.CharFilter(field_name="tid", label="订单号", lookup_expr="contains")
    name = django_filters.CharFilter(field_name="supply_order__receipt_place__receiver_name",
                                     lookup_expr="contains", label="收货人姓名")
    mobile = django_filters.CharFilter(field_name="supply_order__receipt_place__receiver_mobile",
                                       lookup_expr="contains", label="收货人手机号")

    class Meta:
        model = WomaiOrder
        fields = ["order_status", "tid"]
